package SingleListPractice;

import java.util.LinkedList;
import java.util.List;

/**
 * @Author lyq
 * @Description：
 */
public class MyLinkedList {
    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;//标记双向链表的头部
    public ListNode tail;//标记双向链表的尾部


    /**
     * 打印双向链表的每个节点 的值
     */
    public void display(){
        ListNode cur = this.head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur  = cur.next;
        }
        System.out.println();
    }

    /**
     * 得到双向链表的长度
     * @return
     */
    public int size() {
        ListNode cur = this.head;
        int count = 0;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = this.head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;

    }

    /**
     * 头插法
     * 头插要判断链表是否为空
     * 头插和tail没什么关系(尾巴不动头插法)
     *
     * ps:无论什么插入或者删除方法都要保证前后节点的关系不要丢失。
     * ps:插入的时候要先让节点和它后面的节点先绑定上
     * @param data
     */
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        // 1、判断链表是否为空
        if(this.head == null){
            head = node;
            tail = node;
        }else {
            //2、链表不为空就进行头插
            node.next = head;
            head.prev = node;
            head = node;
             }
    }

    //尾插法(头不动插入法)
    public void addLast(int data){
        ListNode node = new ListNode(data);
        // 1、判断链表是否为空
        if(this.head == null){
            this.head = node;
            this.tail = node;
        }else {
            //2、链表不为空就进行尾插
           tail.next = node;
           node.prev = tail;
           tail = node;
        }

    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        //1、判断Index位置的合法性
        if(index<0 || index >size()){
            throw new IndexWrongException("插入位置不合法!");
        }

        //2、判断特殊位置，头插 和 尾插
        if(index == 0){
            addFirst(data);//进行头插法
            return;
        }
        if(index == size()){
            addLast(data);//进行尾巴插入
            return;
        }
        //3、找到index位置节点的地址
        ListNode cur = findIndexListNode(index);
        //4、修改4个指向
        ListNode node = new ListNode(data);
        node.next = cur;
        node.prev = cur.prev;
        cur.prev.next = node;
        cur.prev = node;

    }

    private ListNode findIndexListNode(int index) {
        ListNode cur = this.head;
        while(index != 0){
            index--;
            cur = cur.next;
        }
        return cur;

    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur =this.head;
        while(cur != null){
            if(cur.val == key){
                if(cur == this.head){
                    this.head = this.head.next;
                    if(this.head != null){
                        this.head.prev = null;
                    }
                    else{
                        tail = null;
                    }
                }
                else{
                    cur.prev.next = cur.next;
                    if(cur.next != null){
                        cur.next.prev = cur.prev;
                    }else{
                        this.tail =  cur.prev;
                    }

                }
                return ;
            }
            cur = cur.next;
        }

    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode cur = head;
        while (cur != null) {
            if(cur.val == key) {
                if(cur == head) {
                    head = head.next;
                    if(head != null) {
                        head.prev = null;
                    }else {
                        tail = null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    if(cur.next != null) {
                        cur.next.prev = cur.prev;
                    }else {
                        this.tail = cur.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }

    public void clear(){
        ListNode cur = this.head;
        while(cur != null){
            ListNode curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        head = null;
        tail = null;
    }
}